package com.flea.hclv.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flea.hclv.model.dao.PersonCardInfoMapper;
import com.flea.hclv.model.entity.PersonCardInfo;
import com.flea.hclv.service.PersonCardInfoService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.utils.FleaStringUtils;
@Service
public class PersonCardInfoServiceImpl implements PersonCardInfoService {
	@Autowired
	private PersonCardInfoMapper personCardInfoMapper;
	@Override
	public ServerResponse<String> saveOrUpdate(PersonCardInfo personCardInfo) {
		if (personCardInfo==null){
            return ServerResponse.createByErrorMessage("信息为空");
        }
        String id = personCardInfo.getId();
        String wxNo = personCardInfo.getWxNo();
        if(StringUtil.isEmpty(wxNo)) {
        	 return ServerResponse.createByErrorMessage("微信号为空");
        }
        PersonCardInfo oldPerCard =	personCardInfoMapper.findByWxNo(wxNo);

        if(oldPerCard!=null) {
        	id = oldPerCard.getId();
        }
        
        int result;
        if (StringUtil.isEmpty(id)) {
        	
        	
        	
        	
        	//获取身份证号码，然后判断系统中该身份证用户的人员是否已经存在
        	
            id = FleaStringUtils.getGenerateUUID();
            personCardInfo.setId(id);
           
            personCardInfo.setCreateUser("weixin");
            personCardInfo.setModifyUser("weixin");
            personCardInfo.setValid("Y");
           
            result = personCardInfoMapper.insertSelective(personCardInfo);
            if (result<0){
                return ServerResponse.createByErrorMessage("人员添加失败");
            }
        }else {
        	//需要判断哪些信息可以修改，哪些信息不可以修改
     
        	 personCardInfo.setCreateUser("weixin");
             personCardInfo.setModifyUser("weixin");
             personCardInfo.setValid("Y");
            result = personCardInfoMapper.updateByPrimaryKeySelective(personCardInfo);
            if (result<0){
                return ServerResponse.createByErrorMessage("人员更新失败");
            }
        }
        return ServerResponse.createBySuccessMessage("操作成功");
	}
	@Override
	public ServerResponse<PersonCardInfo> findByWxNo(String wxNo) {
	PersonCardInfo result =	personCardInfoMapper.findByWxNo(wxNo);
	return ServerResponse.createBySuccess(result);
	}

}
